package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.system.domain.cm.CmLoginLog;
import cn.flying.system.domain.cm.CmLoginLogExample;
import cn.flying.system.mapper.cm.CmLoginLogMapper;
import cn.flying.system.service.cm.CmLoginLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @description: 登录日志服务
 * @author: lvyong
 * @date: 2023年05月31日 13:34
 * @version: 1.0
 */
@Service
public class CmLoginLogServiceImpl implements CmLoginLogService {

    @Resource
    private CmLoginLogMapper cmLoginLogMapper;

    /**
     * 分页查询登录日志
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResponse<CmLoginLog> getLoginLogPageList(PageRequest<CmLoginLog> pageRequest) {
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        CmLoginLog cmLoginLog = pageRequest.getParam();
        CmLoginLogExample example = new CmLoginLogExample();
        CmLoginLogExample.Criteria criteria = example.createCriteria();
        if (StringUtils.hasText(cmLoginLog.getUserId())) {
            criteria.andUserIdEqualTo(cmLoginLog.getUserId());
        }
        if (StringUtils.hasText(cmLoginLog.getUserName())) {
            criteria.andUserNameEqualTo(cmLoginLog.getUserName());
        }
        example.setOrderByClause(" created_time desc ");
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        Page<CmLoginLog> page = (Page<CmLoginLog>) cmLoginLogMapper.selectByExample(example);
        return new PageResponse(page.getTotal(), pageNum, page.getResult());
    }
}
